﻿using System;
using System.Windows.Forms;

namespace CP2011_Assignment
{
    public partial class CheckInvUI : Form
    {
        public CheckInvUI()
        {
            
            InitializeComponent();
            checkInventory(new InventorySystem());
            txtCheckInvQuest2.Hide();
            lblEnterID.Hide();
            lblEnterQty.Hide();
            btnExecute.Hide();
            btnCancel.Hide();
            txtBoxInventory.Hide();
            txtBoxQty.Hide();
        }

       
        private void CheckInventoryUI_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'assignmentDBDataSet2.Inventory' table. You can move, or remove it, as needed.
            this.inventoryTableAdapter2.Fill(this.assignmentDBDataSet2.Inventory);
            

        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Owner.Show();
            this.Dispose();


        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            txtCheckUIQuest.Hide();
            btnNo.Hide();
            btnYes.Hide();
            txtCheckInvQuest2.Show();
            lblEnterID.Show();
            lblEnterQty.Show();
            btnExecute.Show();
            btnCancel.Show();
            txtBoxInventory.Show();
            txtBoxQty.Show();

        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtCheckInvQuest2.Hide();
            lblEnterID.Hide();
            lblEnterQty.Hide();
            btnExecute.Hide();
            btnCancel.Hide();
            txtBoxInventory.Hide();
            txtBoxQty.Hide();
            txtCheckUIQuest.Show();
            btnNo.Show();
            btnYes.Show();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            ///instantiate struct to store temporary data
            if (txtBoxInventory.Text.Equals("") || txtBoxQty.Text.Equals(""))
            {
                MessageBox.Show("Invalid Value, field can't be empty!", "Field Can't be Empty");
            }
            else
            {
                int x;
                bool intCheck1 = Int32.TryParse(txtBoxInventory.Text, out x);
                bool intCheck2 = Int32.TryParse(txtBoxQty.Text, out x);
                if (intCheck1 == true && intCheck2 == true)
                {
                    String editQuery = "UPDATE Inventory SET Inv_QtyOnHand =" + Convert.ToInt32(txtBoxQty.Text) + "WHERE Inv_ID = " + Convert.ToInt32(txtBoxInventory.Text);

                    executeChange(new InventorySystem(), editQuery);

                    this.Hide();
                    this.Owner.Show();
                    this.Dispose();
                }
                else
                {
                     MessageBox.Show("Input format is incorrect, please insert only integer value ENTER ID", "Incorrect Input Format", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Show();
                
                }
            }
        }

        /// <summary>
        /// DIP
        /// </summary>
        /// <param name="dataCtrl"></param>
        /// <param name="tempStrge"></param>
        private void executeChange(IDataController dataCtrl, String editQuery)
        {

            if (MessageBox.Show("Do you want to change quantity of Item ID: " + Convert.ToInt32(txtBoxInventory.Text) + " to: " + Convert.ToInt32(txtBoxQty.Text) + "kg", "Confirm update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                dataCtrl.editRecord(editQuery, Convert.ToInt32(txtBoxInventory.Text));
            }

        }

        private void checkInventory(IDataController dataCtrl)
        {
            String checkQuery = "SELECT Inv_ID FROM Inventory WHERE Inv_QtyOnHand < 200";
            dataCtrl.checkRecord(checkQuery);
        }

        private void CheckInvUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            this.Owner.Show();
            this.Dispose();
        }


    }


}

